<template>
    <el-dialog title="关联考试" :close-on-click-modal="false" :visible.sync="visible" width="60%" append-to-body>¬
        <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
            <!--<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
            <el-table-column align="center" type="index" header-align="center" width="50px"
                             label="序号"></el-table-column>
            <el-table-column prop="name" header-align="center" align="center" label="考试名称"></el-table-column>
            <el-table-column prop="duration" header-align="center" align="center" width="110px"
                             label="考试时长(分钟)"></el-table-column>
            <el-table-column prop="passMark" header-align="center" align="center" width="70px"
                             label="及格分数"></el-table-column>
            <el-table-column prop="startTime" header-align="center" align="center" label="开始时间"></el-table-column>
            <el-table-column prop="endTime" header-align="center" align="center" label="结束时间"></el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="50" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="dataFormSubmit(scope.row)">选择</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="pageData.pageIndex"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageData.pageSize"
                layout="sizes, prev, pager, next"
                :total="pageData.pageTotal">
        </el-pagination>
    </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        dataListLoading: false,
        dataList: [],
        dataForm: {
          id: '',
          examId: ''
        },
        pageData: {
          pageSize: 10,
          pageIndex: 1,
          pageTotal: 0
        }
      }
    },
    methods: {
      handleSizeChange (val) {
        this.pageData.pageSize = val
        this.getDataList()
      },
      handleCurrentChange (val) {
        this.pageData.pageIndex = val
        this.getDataList()
      },
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/exam-admin/exam/list'),
          method: 'post',
          data: {
            page: this.pageData.pageIndex,
            limit: this.pageData.pageSize
          }
        }).then(data => {
          if (data && data.code === 200) {
            this.dataList = data.data.list
            this.pageData.pageTotal = data.data.totalCount
          } else {
            this.dataList = []
            this.pageData.pageTotal = 0
          }
          this.dataListLoading = false
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.getDataList()
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
        // })
      },
      dataFormSubmit (row) {
        this.dataForm.examId = row.id
        this.loading = true
        this.$http({
          url: this.$http.adornUrl('/train/admin/train/examaftertrain'),
          method: 'post',
          data: {
            'trainId': this.dataForm.id,
            'examId': row.id
          }
        }).then(data => {
          if (data && data.code === 200) {
            this.$message({
              'message': '操作成功',
              'type': 'success',
              'duration': 1500
            })
            this.$emit('refreshDataList')
            this.visible = false
          } else {
            this.$message.error(data.msg)
          }
          this.loading = false
        })
      }
    }
  }
</script>

<style scoped>

</style>
